<?php
namespace app\api\logic;

use app\basic\BaseLogic;
use app\api\model\UserFootprint;
use app\basic\exception\ApiException;

/**
 * 用户足迹逻辑层
 */
class UserFootprintLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new UserFootprint();
    }

    /**
     * 添加足迹
     * @param int $userId 用户ID
     * @param int $articleId 文章ID
     * @param string $title 标题
     * @param string $image 图片
     * @return bool
     */
    public function addFootprint(int $userId, int $articleId, string $title, string $image = ''): bool
    {
        // 检查是否已存在
        $exist = $this->where([
            'user_id' => $userId,
            'article_id' => $articleId
        ])->findOrEmpty();

        if (!$exist->isEmpty()) {
            // 已存在则更新浏览时间
            return $this->where('id', $exist['id'])->update([
                'update_time' => time()
            ]) > 0;
        }

        // 不存在则新增
        return $this->save([
            'user_id' => $userId,
            'article_id' => $articleId,
            'title' => $title,
            'image' => $image,
            'create_time' => time(),
            'update_time' => time()
        ]) > 0;
    }

    /**
     * 获取足迹列表
     * @param int $userId 用户ID
     * @param int $page 页码
     * @param int $limit 每页数量
     * @return array
     */
    public function getFootprintList(int $userId, int $page = 1, int $limit = 10): array
    {
        // 查询列表
        $query = $this->where('user_id', $userId)
            ->field('id,article_id,title,create_time,view_count')
            ->order('update_time DESC');

        // 分页查询
        $result = $query->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);

        return [
            'list' => $result->items(),
            'total' => $result->total(),
            'page' => $result->currentPage(),
            'limit' => $limit
        ];
    }

    /**
     * 删除足迹
     * @param int $userId 用户ID
     * @param array $ids 足迹ID数组
     * @return bool
     */
    public function deleteFootprint(int $userId, array $ids): bool
    {
        if (empty($ids)) {
            throw new ApiException('请选择要删除的足迹');
        }

        return $this->where('user_id', $userId)
            ->whereIn('id', $ids)
            ->delete() > 0;
    }

    /**
     * 获取足迹统计
     * @param int $userId 用户ID
     * @return array
     */
    public function getFootprintCount(int $userId): array
    {
        // 获取今日开始和结束时间戳
        $todayStart = strtotime(date('Y-m-d 00:00:00'));
        $todayEnd = strtotime(date('Y-m-d 23:59:59'));
        
        // 查询今日浏览数
        $todayCount = $this->where([
            'user_id' => $userId,
        ])->whereBetween('update_time',"$todayStart,$todayEnd")
        ->count();

        // 查询总浏览数
        $totalCount = $this->where([
            'user_id' => $userId
        ])->count();
        
        return [
            'today_count' => $todayCount,
            'total_count' => $totalCount
        ];
    }
} 